﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Net;
using System.IO;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour
{
	void Start()
	{
		bool ss = FileUpload(Path.Combine(Application.dataPath, "dongman.jpg"), "ftp://www.v.online/1.jpg");
		UnityEngine.Debug.Log("上传结果:" + ss);
	}

	public bool FileUpload(string localpath, string url)
	{
		bool flag = false;

		FtpWebRequest reqFTP = null;
		FileStream localFileStream = null;
		Stream requestStream = null;

		try
		{
			FileInfo localfile = new FileInfo(localpath);

			reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));

			reqFTP.Credentials = new NetworkCredential("dingxiaowei", "dingxiaowei");
			reqFTP.UsePassive = true;
			reqFTP.UseBinary = true;
			reqFTP.KeepAlive = false;

			reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

			//本地文件流
			localFileStream = localfile.OpenRead();

			int Length = 1024;
			byte[] buff = new byte[Length];
			int contentLen;

			contentLen = localFileStream.Read(buff, 0, Length);

			//ftp数据流
			requestStream = reqFTP.GetRequestStream();

			while (contentLen != 0)
			{
				requestStream.Write(buff, 0, Length);
				contentLen = localFileStream.Read(buff, 0, Length);

			}
			flag = true;
		}
		catch (Exception ex)
		{
			Debug.Log(ex.Message);
			flag = false;
		}
		finally
		{
			if (localFileStream != null)
				localFileStream.Close();

			if (requestStream != null)
				requestStream.Close();
		}
		return flag;
	}
}
